package com.third.clock.ui.model.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.bean.ReportEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.ui.model.IMindsFragmentView;
import com.third.clock.ui.model.INoticeListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class NoticeListPresenter {

    private Context mContext;

    private INoticeListView mView;

    private OkHttpClientManager mOkHttpClientManager;

    private ClockManager clockManager;

    private int pageindex=1;

    private int pagesize=10;

    public enum LoadType{
        LOADMORE,REFRESH;
    }

    public NoticeListPresenter(Context context){
        this.mContext=context;
        clockManager=ClockManager.getInstace(mContext);
        mOkHttpClientManager=clockManager.getOkHttpClientManager();
    }

    public void setINoticeListView(INoticeListView view){
        this.mView=view;
    }

    public void onRefresh(final LoadType type){
        ResponseData<LoginEntity> responseData=clockManager.mLoginResponseData;
        if(responseData==null){
            return;
        }
        if(responseData.getData()==null){
            return;
        }
        if(mView!=null) mView.startRefreshing();
        Map<String,Object> getreportMap=new HashMap<>();
        getreportMap.put("userid",responseData.getData().getUser_id());
        if(type== LoadType.REFRESH){
            pageindex=1;
        }else{
            ++pageindex;
        }
        getreportMap.put("pageindex", pageindex);
        getreportMap.put("pagesize",pagesize);

        String toSign= TransformManager.sortMapByKey(getreportMap);
        getreportMap.put("sign",toSign);

        mOkHttpClientManager.post(Contants.GETLISTNOTICE_URL,getreportMap, new OkHttpClientManager.okHttpCallback() {
            @Override
            public void onFailure(com.lzy.okgo.model.Response<String> s) {
                if (mView != null) {
                    if(type== LoadType.REFRESH) {
                        mView.stopRefreshing();
                    }else{
                        mView.onLoadmorestop();
                    }
                }
            }

            @Override
            public void onResponse(com.lzy.okgo.model.Response<String> response) {
                if(type== LoadType.REFRESH) {
                    mView.stopRefreshing();
                }else{
                    mView.onLoadmorestop();
                }
                if(response!=null&&response.body()!=null){
                    ResponseData<List<NoticeEntity>> responseData=TransformManager.paserListNoticeEntity(response.body());
                    if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                        List<NoticeEntity> list=responseData.getData();
                        if (mView != null) mView.onRefreshData(type,list);
                    }
                }
            }
        });
    }

}
